import { instantiate, Node, Prefab, v3, Vec3 } from "cc";
import { PoolMgr } from "../../framework/manager/PoolMgr";
import { ResourceMgr } from "../../framework/manager/ResourceMgr";
import { Utils } from "../../framework/utils/Utils";
import { Main } from "../../Main";
import { UIMgr } from "../../framework/manager/UIMgr";
import { AudioId, UIID } from "../config/Config";
import { PlayerMgr } from "./PlayerMgr";
import { AudioMgr } from "../../framework/manager/AudioMgr";


/**
 * 游戏管理类
 */
export default class GameMgr {
    private constructor() { }
    private static _instance: GameMgr;
    public static getInstance(): GameMgr {
        if (this._instance == null) {
            this._instance = new GameMgr();
        }
        return this._instance;
    }
    /**动画时间 */
    aniTime: number = 1
    //玩家初始坐标
    startPos_player: Vec3 = v3(0, 0, 0);
    /**冰冻时间 60 */
    spaceTime: number = 30
    /**玩家关卡 */
    levelData: number = 0
    //游戏是否结束
    isEndGame: boolean = false;
    //当前关卡的解锁id
    levelClockId: number = 0;


    /**开局技能列表 */
    gameStarSkinLst: number[] = [-1, -1, -1];

    setGameSkin(idx: number, num: number) {
        this.gameStarSkinLst[idx] = num
    }
    getGameSkin() {
        return this.gameStarSkinLst
    }
    initGameSkin() {
        this.gameStarSkinLst = [-1, -1, -1]
    }


    //设置id
    setClockId(id: number) {
        this.levelClockId = id;
    }

    //返回id
    getClockId() {
        return this.levelClockId
    }
    //丢金币
    throwCoin(node: Node, iCount: number) {
        let iCount_Coin = 1;
        if (iCount >= 1 && iCount <= 5) {
            iCount_Coin = 1;
        }
        else if (iCount >= 6 && iCount <= 10) {
            iCount_Coin = 2;
        }
        else if (iCount >= 11 && iCount <= 50) {
            iCount_Coin = 3;
        }
        else if (iCount >= 51 && iCount <= 100) {
            iCount_Coin = 4;
        }
        else if (iCount >= 101) {
            iCount_Coin = 5;
        }
        let endCount = iCount % iCount_Coin;
        for (let index = 0; index < iCount_Coin; index++) {
            //丢金币
            let posX = Utils.randomRang(-10, 10) + node.position.x;
            let posZ = Utils.randomRang(-10, 10) + node.position.z;
            let posCoin = v3(posX, 3, posZ);
            let nodeCoin = PoolMgr.getInstance().getNode("Coin");
            let coinCount = Math.floor(iCount / iCount_Coin);
            if (endCount > index) {
                coinCount += 1;
            }
            // console.log("coinCount：", coinCount);
            if (nodeCoin) {
                nodeCoin.parent = Main.instance.node;
                nodeCoin.position = posCoin;
                // nodeCoin.getComponent(Coin).initData(coinCount);
            }
            else {
                ResourceMgr.getInstance().loadRes("object", "Coin", Prefab, (prefab: Prefab) => {
                    nodeCoin = instantiate(prefab);
                    nodeCoin.position = posCoin;
                    // nodeCoin.getComponent(Coin).initData(coinCount);
                });
            }
        }
    }

    //设置游戏状态
    setGameState(isEnd: boolean) {
        this.isEndGame = isEnd;
    }
    //获取游戏状态
    getGameState() {
        return this.isEndGame;
    }

    //游戏结束
    public EndGame(levelId: number, isWin: boolean = false, tyNum: number, isChange: boolean) {
        this.isEndGame = true;
        // if (UIMgr.getInstance().getUIById(UIID.UIGameTip)) {
        //     UIMgr.getInstance().closeById(UIID.UIGameTip);
        // }
        // if (isWin) PlayerMgr.getInstance().getUserData().setUnlockLevel(levelId + 1);
        UIMgr.getInstance().openUI(UIID.UISettle, UIID.Default, levelId, isWin, tyNum, isChange);
    }

    //返回主页
    public BackHome() {
        AudioMgr.getInstance().stopMusic("GameBGM");
        AudioMgr.getInstance().playMusic(AudioId.mainBgm, "MainBGM");
        // UIMgr.getInstance()
        UIMgr.getInstance().openUI(UIID.UILoad, UIID.Default);
        UIMgr.getInstance().openUI(UIID.UIHome, UIID.Default);
    }

    //返回选关页面
    public BackSelect() {
        AudioMgr.getInstance().stopMusic("GameBGM");
        AudioMgr.getInstance().playMusic(AudioId.mainBgm, "MainBGM");
        GameMgr.getInstance().BackHome();
    }
}